package com.top.horizon.th1804app.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.net.protocol.NetController;
import com.net.protocol.model.CommonModel;
import com.net.protocol.model.DeviceInfoModel;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.adapter.FramesItemTouchHelperCallback;
import com.top.horizon.th1804app.adapter.MoreFramesAdapter;
import com.top.horizon.th1804app.adapter.OnRecyclerItemClickListener;
import com.top.horizon.th1804app.base.BaseFragment;
import com.top.horizon.th1804app.constant.Constant;
import com.top.horizon.th1804app.ui.activity.ChangePasswordActivity;
import com.top.horizon.th1804app.ui.activity.LoginActivity;
import com.top.horizon.th1804app.utils.LogUtil;
import com.top.horizon.th1804app.utils.SPUtils;
import com.top.horizon.th1804app.view.CustomDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class SettingFragment extends BaseFragment implements View.OnClickListener,
        RadioGroup.OnCheckedChangeListener, CustomDialog.OnClickChildVIewListener,
        CompoundButton.OnCheckedChangeListener, View.OnLongClickListener,
        View.OnTouchListener, MoreFramesAdapter.OnTouchFramesListener {

    private RadioGroup rgFrames;
    private LinearLayout llSingleFrames;
    private TextView tvSingleFrames, tvChannel1, tvChannel2, tvChannel3, tvChannel4;
    private EditText etTagContent;
    private CheckBox cbHighDefinition, cbStandardDefinition1, cbStandardDefinition2, cbStandardDefinition3, cbStandardDefinition4;

    private DeviceInfoModel deviceInfoModel;
    private RadioGroup rgSelectChannel;
    private LinearLayout llChannel1, llChannel2, llChannel3, llChannel4;
    private long downTime;
    private RelativeLayout fl;
    //需要添加标签的通道，1为需要，0为不需要  通道号对应：0：标清1 ,1：标清2 , 2：标清3 , 3：标清4 , 4：高清
    private int[] tagChannel = new int[]{1, 1, 1, 1, 1};
    private int[] channelNumbers;
    //当前正在实时播放的通道  前四位是多画面的四个通道，第五个是单画面的通道
    private int[] checkedChannelNumbers = new int[5];
    private RecyclerView rvFrames;
    private MoreFramesAdapter myAdapter;
    private ArrayList<Integer> dataList = new ArrayList<>();
//    private String[] channelNames = new String[]{"标清视频1", "标清视频2", "标清视频3", "标清视频4", "高清视频"};

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void initView() {
        //单画面通道
        tvSingleFrames = rootView.findViewById(R.id.tv_single_frames);
        llSingleFrames = rootView.findViewById(R.id.ll_single_frames);
        fl = rootView.findViewById(R.id.rl);
        //多画面通道
        rvFrames = rootView.findViewById(R.id.rv_frames);

        tvChannel1 = rootView.findViewById(R.id.tv_channel_1);
        tvChannel2 = rootView.findViewById(R.id.tv_channel_2);
        tvChannel3 = rootView.findViewById(R.id.tv_channel_3);
        tvChannel4 = rootView.findViewById(R.id.tv_channel_4);
        //单多切换按钮
        rgFrames = rootView.findViewById(R.id.rg_frames);
        //标签输入框
        etTagContent = rootView.findViewById(R.id.et_tag_content);
        //通道选择框
        cbHighDefinition = rootView.findViewById(R.id.cb_high_definition);
        cbStandardDefinition1 = rootView.findViewById(R.id.cb_standard_definition1);
        cbStandardDefinition2 = rootView.findViewById(R.id.cb_standard_definition2);
        cbStandardDefinition3 = rootView.findViewById(R.id.cb_standard_definition3);
        cbStandardDefinition4 = rootView.findViewById(R.id.cb_standard_definition4);
        llChannel1 = rootView.findViewById(R.id.ll_channel_1);
        llChannel2 = rootView.findViewById(R.id.ll_channel_2);
        llChannel3 = rootView.findViewById(R.id.ll_channel_3);
        llChannel4 = rootView.findViewById(R.id.ll_channel_4);
        llChannel1.setTag(0);
        llChannel2.setTag(1);
        llChannel3.setTag(2);
        llChannel4.setTag(3);

        //设置监听
        rgFrames.setOnCheckedChangeListener(this);
        //点击监听
        rootView.findViewById(R.id.btn_add_tag).setOnClickListener(this);
        rootView.findViewById(R.id.btn_clear).setOnClickListener(this);
        rootView.findViewById(R.id.btn_quit_login).setOnClickListener(this);
        rootView.findViewById(R.id.btn_change).setOnClickListener(this);
        //通道号选择监听
        cbHighDefinition.setOnCheckedChangeListener(this);
        cbStandardDefinition1.setOnCheckedChangeListener(this);
        cbStandardDefinition2.setOnCheckedChangeListener(this);
        cbStandardDefinition3.setOnCheckedChangeListener(this);
        cbStandardDefinition4.setOnCheckedChangeListener(this);
        //单画面长按监听
        llSingleFrames.setOnClickListener(this);
        //多画面触摸监听
        llChannel1.setOnTouchListener(this);
        llChannel2.setOnTouchListener(this);
        llChannel3.setOnTouchListener(this);
        llChannel4.setOnTouchListener(this);
    }

    private MoreFramesAdapter.MoreFramesViewHolder moreFramesViewHolder;

    @Override
    protected void initData() {
        LogUtil.e("SettingFragment", "onCreate");
        channelNumbers = SPUtils.getCheckedChannels(getContext(), Constant.CHECKED_CHANNEL);
        setChannelName(channelNumbers);
        for (int i = 0; i < channelNumbers.length; i++) {
            Log.e("adapter",i+"==="+Constant.channelNames[channelNumbers[i]]);
//            channelNumbers[i] = mData.get(i);
        }
        int checkedFrames = (int) SPUtils.get(getContext(), Constant.CHECKED_FRAMES, -1);
        if ((checkedFrames == -1)) {
            rgFrames.check(R.id.rb_single_frames);
        } else {
            rgFrames.check(checkedFrames);
        }
        deviceInfoModel = new DeviceInfoModel();
        setAdapter();


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add_tag:
                //点击添加标签
                String tagContent = etTagContent.getText().toString().trim();
                if (!TextUtils.isEmpty(tagContent)) {
                    //如果标签不为空，就将标签发给服务器保持
                    showProgressLoadingDialog();
                    NetController.addTAG(tagChannel, tagContent);
                }
                etTagContent.setText("");
                break;
            case R.id.btn_clear:
                //点击清空硬盘
                showChooseDialog(R.layout.dialog_clear_disk);
                break;
                case R.id.ll_single_frames:
                    //判断当前是回放状态还是实时状态
                    if (deviceInfoModel.playStatus == 1) {
                        //回放状态提示不可切换
                        showChooseDialog(R.layout.dialog_forget_password);
                    } else {
                        showChooseDialog(R.layout.dialog_select_channel);
                    }
                break;
            case R.id.btn_change:
                //点击修改密码
                startActivity(new Intent(getActivity(), ChangePasswordActivity.class));
                break;
            case R.id.btn_quit_login:
                //点击退出登录
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                break;
            case R.id.btn_cancel:
                //点击对话框取消按钮
                dismissDialog();
                break;
            case R.id.btn_forget_password:
                //点击对话框取消按钮
                dismissDialog();
                break;
            case R.id.btn_confirm_clear:
                dismissDialog();
                //清空硬盘文件，通知服务器
                showProgressLoadingDialog();
                NetController.clearDisk();
                break;
            case R.id.btn_select_channel:
                //点击选择通道对话框确定按钮，将选择的通道号告诉服务器，然后显示选择的通道号
                switch (rgFrames.getCheckedRadioButtonId()) {
                    case R.id.rb_single_frames:
                        //如果当前选择的是单画面播放
                        switch (rgSelectChannel.getCheckedRadioButtonId()) {
                            case R.id.rb_channel_0:
                                channelNumbers[4] = 0;
                                break;
                            case R.id.rb_channel_1:
                                channelNumbers[4] = 1;
                                break;
                            case R.id.rb_channel_2:
                                channelNumbers[4] = 2;
                                break;
                            case R.id.rb_channel_3:
                                channelNumbers[4] = 3;
                                break;
                            case R.id.rb_channel_4:
                                channelNumbers[4] = 4;
                                break;
                        }
                        setChannelName(channelNumbers);
                        //通知服务器更改通道
                        NetController.displayOne(channelNumbers[4]);
                        break;
                    case R.id.rb_more_frames:
                        switch (rgSelectChannel.getCheckedRadioButtonId()) {
                            case R.id.rb_channel_0:
                                channelNumbers[clickChannel] = 0;
                                break;
                            case R.id.rb_channel_1:
                                channelNumbers[clickChannel] = 1;
                                break;
                            case R.id.rb_channel_2:
                                channelNumbers[clickChannel] = 2;
                                break;
                            case R.id.rb_channel_3:
                                channelNumbers[clickChannel] = 3;
                                break;
                            case R.id.rb_channel_4:
                                channelNumbers[clickChannel] = 4;
                                break;
                        }
                        SPUtils.saveCheckedChannels(getContext(), Constant.CHECKED_CHANNEL, channelNumbers);
                        moreFramesViewHolder.tvChannelNumber.setText(Constant.channelNames[channelNumbers[clickChannel]]);
                        NetController.displayMore(channelNumbers[0], channelNumbers[1], channelNumbers[2], channelNumbers[3]);
                        break;
                }
                // 并关闭对话框
                dismissDialog();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        LogUtil.e("SettingFragment", "onResume");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.e("SettingFragment", "onPause");
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
//        myAdapter=null;

    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        myAdapter = new MoreFramesAdapter(getContext(), channelNumbers);
        rvFrames.addItemDecoration(new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL));
        //设置默认的布局方式
        rvFrames.setLayoutManager(new GridLayoutManager(getContext(), 2));
        //设置adapter
        rvFrames.setAdapter(myAdapter);
        //创建SimpleItemTouchHelperCallback
        ItemTouchHelper.Callback callback = new FramesItemTouchHelperCallback(myAdapter);
        //用Callback构造ItemtouchHelper
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        //调用ItemTouchHelper的attachToRecyclerView方法建立联系
        touchHelper.attachToRecyclerView(rvFrames);
        //设置监听
        myAdapter.setOnTouchFramesListener(this);
        rvFrames.addOnItemTouchListener(new OnRecyclerItemClickListener(rvFrames) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                moreFramesViewHolder = (MoreFramesAdapter.MoreFramesViewHolder) vh;
                //判断当前是回放状态还是实时状态
                if (deviceInfoModel.playStatus == 1) {
                    //回放状态提示不可切换
                    showChooseDialog(R.layout.dialog_forget_password);
                } else {
                    clickChannel = vh.getLayoutPosition();
                    showChooseDialog(R.layout.dialog_select_channel);
                }

            }

            @Override
            public void onItemLongClick(RecyclerView.ViewHolder vh) {

            }
        });
    }


    @SuppressLint("ResourceType")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void heartEvent(DeviceInfoModel deviceInfoModel) {
        if (deviceInfoModel.displayStauts == 0) {
            //单路
            rgFrames.check(0);
        } else {
            //多路
            rgFrames.check(1);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void commonEvent(CommonModel commonModel) {
        if (commonModel.code == 0) {
            showToast("操作成功");
        } else {
            showToast("操作失败");
        }
    }


    /**
     * 点击单画面和多画面切换
     *
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_single_frames:
                //选择了单画面
                llSingleFrames.setVisibility(View.VISIBLE);
                //向服务器发送选择单画面的通路  默认高清
                NetController.displayOne(channelNumbers[4]);
                break;
            case R.id.rb_more_frames:
                //选择了多画面
                llSingleFrames.setVisibility(View.GONE);
                //向服务器发送选择多画面的通路  默认高清，标清1，标清2，标清3
                NetController.displayMore(channelNumbers[0], channelNumbers[1], channelNumbers[2], channelNumbers[3]);
                break;
        }
        setChannelName(channelNumbers);
        SPUtils.put(getContext(), Constant.CHECKED_FRAMES, checkedId);
    }

    /**
     * 设置更改后的通道名字
     *
     * @param channelNumbers
     */
    private void setChannelName(int[] channelNumbers) {
        //设置通道名字
        tvSingleFrames.setText(Constant.channelNames[channelNumbers[4]]);
        tvChannel1.setText(Constant.channelNames[channelNumbers[0]]);
        tvChannel2.setText(Constant.channelNames[channelNumbers[1]]);
        tvChannel3.setText(Constant.channelNames[channelNumbers[2]]);
        tvChannel4.setText(Constant.channelNames[channelNumbers[3]]);
        SPUtils.saveCheckedChannels(getContext(), Constant.CHECKED_CHANNEL, channelNumbers);
    }

    /**
     * 对话框确定取消监听
     *
     * @param view
     * @param mLayoutId
     */
    @Override
    public void onClickChildView(View view, int mLayoutId) {
        switch (mLayoutId) {
            case R.layout.dialog_clear_disk:
                view.findViewById(R.id.btn_cancel).setOnClickListener(this);
                view.findViewById(R.id.btn_confirm_clear).setOnClickListener(this);
                break;
            case R.layout.dialog_select_channel:
                rgSelectChannel = view.findViewById(R.id.rg_select_channel);
                view.findViewById(R.id.btn_select_channel).setOnClickListener(this);
                switch (rgFrames.getCheckedRadioButtonId()) {
                    case R.id.rb_single_frames:
                        //如果当前选择的是单画面播放
                        rgSelectChannel.getChildAt(channelNumbers[4]).setVisibility(View.GONE);
                        break;
                    case R.id.rb_more_frames:
                        //当前是多画面播放
                        for (int i = 0; i < 4; i++) {
                            LogUtil.e("數組大俠", "=====" + channelNumbers[i]);
                            rgSelectChannel.getChildAt(channelNumbers[i]).setVisibility(View.GONE);
                        }
                        break;
                }
                break;
            case R.layout.dialog_forget_password:
                TextView tvForget = view.findViewById(R.id.tv_forget);
                tvForget.setText("当前是回放状态\n不可以切换通道");
                view.findViewById(R.id.btn_forget_password).setOnClickListener(this);
                break;
        }
    }

    /**
     * 添加标签选择通道
     *
     * @param buttonView
     * @param isChecked
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_high_definition:
                if (isChecked) {
                    tagChannel[4] = 1;
                } else {
                    tagChannel[4] = 0;
                }
                break;
            case R.id.cb_standard_definition1:
                if (isChecked) {
                    tagChannel[0] = 1;
                } else {
                    tagChannel[0] = 0;
                }
                break;
            case R.id.cb_standard_definition2:
                if (isChecked) {
                    tagChannel[1] = 1;
                } else {
                    tagChannel[1] = 0;
                }
                break;
            case R.id.cb_standard_definition3:
                if (isChecked) {
                    tagChannel[2] = 1;
                } else {
                    tagChannel[2] = 0;
                }
                break;
            case R.id.cb_standard_definition4:
                if (isChecked) {
                    tagChannel[3] = 1;
                } else {
                    tagChannel[3] = 0;
                }
                break;
        }
    }

    /**
     * 长按通道监听
     *
     * @param v
     * @return
     */
    @Override
    public boolean onLongClick(View v) {
        //判断当前是回放状态还是实时状态
        if (deviceInfoModel.playStatus == 1) {
            //回放状态提示不可切换
            showChooseDialog(R.layout.dialog_forget_password);
        } else {
            showChooseDialog(R.layout.dialog_select_channel);
        }
        return false;
    }

    float downX;
    float downY;
    private int left;
    private int top;
    private int right;
    private int bottom;
    private int needChangeChannel = 0;
    int temp = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downTime = System.currentTimeMillis();
                downX = event.getX();
                downY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = event.getX();
                float endY = event.getY();
                float moveX = endX - downX;
                float moveY = endY - downY;
                switch ((int) v.getTag()) {
                    case 0:
                        if (moveX > 0.0 && moveX < llChannel1.getWidth()) {
                            left = (int) moveX;
                            right = (int) (llChannel1.getWidth() + moveX);
                        } else if (moveX <= 0.0) {
                            left = 0;
                            right = llChannel1.getWidth();
                        } else {
                            right = fl.getWidth();
                            left = llChannel1.getWidth();
                        }
                        if (moveY > 0.0 && moveY < llChannel1.getHeight()) {
                            top = (int) moveY;
                            bottom = (int) (llChannel1.getHeight() + moveY);
                        } else if (moveY <= 0.0) {
                            bottom = llChannel1.getHeight();
                            top = 0;
                        } else {
                            bottom = fl.getHeight();
                            top = llChannel1.getHeight();
                        }
                        llChannel1.layout(left, top, right, bottom);
                        break;
                    case 1:
                        if (moveX < 0.0 && moveX > -llChannel1.getWidth()) {
                            left = (int) moveX + fl.getWidth() - llChannel2.getWidth();
                            right = (int) (fl.getWidth() + moveX);
                        } else if (moveX >= 0.0) {
                            left = fl.getWidth() - llChannel2.getWidth();
                            right = fl.getWidth();
                        } else {
                            right = llChannel2.getWidth();
                            left = 0;
                        }
                        if (moveY > 0.0 && moveY < llChannel1.getHeight()) {
                            top = (int) moveY;
                            bottom = (int) (llChannel1.getHeight() + moveY);
                        } else if (moveY <= 0.0) {
                            bottom = llChannel1.getHeight();
                            top = 0;
                        } else {
                            bottom = fl.getHeight();
                            top = llChannel1.getHeight();
                        }
                        llChannel2.layout(left, top, right, bottom);
                        break;
                    case 2:
                        if (moveX > 0.0 && moveX < llChannel3.getWidth()) {
                            left = (int) moveX;
                            right = (int) (llChannel3.getWidth() + moveX);
                        } else if (moveX <= 0.0) {
                            left = 0;
                            right = llChannel3.getWidth();
                        } else {
                            right = fl.getWidth();
                            left = llChannel3.getWidth();
                        }
                        if (moveY < 0.0 && moveY > -llChannel3.getHeight()) {
                            top = (int) moveY + fl.getHeight() - llChannel3.getHeight();
                            bottom = (int) (fl.getHeight() + moveY);
                        } else if (moveY >= 0.0) {
                            bottom = fl.getHeight();
                            top = fl.getHeight() - llChannel3.getHeight();
                        } else {
                            bottom = llChannel3.getHeight();
                            top = 0;
                        }
                        llChannel3.layout(left, top, right, bottom);
                        break;
                    case 3:
                        if (moveX < 0.0 && moveX > -llChannel2.getWidth()) {
                            left = (int) moveX + fl.getWidth() - llChannel2.getWidth();
                            right = (int) (fl.getWidth() + moveX);
                        } else if (moveX >= 0.0) {
                            left = fl.getWidth() - llChannel2.getWidth();
                            right = fl.getWidth();
                        } else {
                            right = llChannel2.getWidth();
                            left = 0;
                        }
                        if (moveY < 0.0 && moveY > -llChannel3.getHeight()) {
                            top = (int) moveY + fl.getHeight() - llChannel3.getHeight();
                            bottom = (int) (fl.getHeight() + moveY);
                        } else if (moveY >= 0.0) {
                            bottom = fl.getHeight();
                            top = fl.getHeight() - llChannel3.getHeight();
                        } else {
                            bottom = llChannel3.getHeight();
                            top = 0;
                        }
                        llChannel4.layout(left, top, right, bottom);
                        break;
                }
                break;
            case MotionEvent.ACTION_UP:
                float upX = event.getX();
                float upY = event.getY();
                float up_move_x = upX - downX;
                float up_move_y = upY - downY;
                //移动小于2时并且按下抬起时间超过1.5秒说明没有移动通道交换位置，然后弹出对话框，选择通道
                if ((up_move_x) < 2 && (up_move_y) < 2 && (System.currentTimeMillis() - downTime) > 1000) {
                    //判断当前是回放状态还是实时状态
                    if (deviceInfoModel.playStatus == 1) {
                        //回放状态提示不可切换
                        showChooseDialog(R.layout.dialog_forget_password);
                    } else {
                        needChangeChannel = (int) v.getTag();
                        showChooseDialog(R.layout.dialog_select_channel);
                    }
                } else {
                    //说明移动交换位置了，需要更改名字
                    switch ((int) v.getTag()) {
                        case 0:
                            if (up_move_x > fl.getWidth() / 4) {
                                if (up_move_y > fl.getHeight() / 4) {
                                    //说明移动到右下
                                    temp = channelNumbers[0];
                                    channelNumbers[0] = channelNumbers[3];
                                    channelNumbers[3] = temp;
                                    temp = 0;
                                } else {
                                    //说明移动到右上
                                    temp = channelNumbers[0];
                                    channelNumbers[0] = channelNumbers[1];
                                    channelNumbers[1] = temp;
                                    temp = 0;
                                }
                            } else {
                                if (up_move_y > fl.getHeight() / 4) {
                                    //说明移动到左下
                                    temp = channelNumbers[0];
                                    channelNumbers[0] = channelNumbers[2];
                                    channelNumbers[2] = temp;
                                    temp = 0;
                                }
                            }
                            break;
                        case 1:
                            if (-up_move_x > fl.getWidth() / 4) {
                                if (up_move_y > fl.getHeight() / 4) {
                                    //说明移动到左下
                                    temp = channelNumbers[1];
                                    channelNumbers[1] = channelNumbers[2];
                                    channelNumbers[2] = temp;
                                    temp = 0;
                                } else {
                                    //说明移动到左上
                                    temp = channelNumbers[1];
                                    channelNumbers[1] = channelNumbers[0];
                                    channelNumbers[0] = temp;
                                    temp = 0;
                                }
                            } else {
                                if (up_move_y > fl.getHeight() / 4) {
                                    //说明移动到右下
                                    temp = channelNumbers[1];
                                    channelNumbers[1] = channelNumbers[3];
                                    channelNumbers[3] = temp;
                                    temp = 0;
                                }
                            }
                            break;
                        case 2:
                            if (up_move_x > fl.getWidth() / 4) {
                                if (-up_move_y > fl.getHeight() / 4) {
                                    //说明移动到右上
                                    temp = channelNumbers[2];
                                    channelNumbers[2] = channelNumbers[1];
                                    channelNumbers[1] = temp;
                                    temp = 0;
                                } else {
                                    //说明移动到右下
                                    temp = channelNumbers[2];
                                    channelNumbers[2] = channelNumbers[3];
                                    channelNumbers[3] = temp;
                                    temp = 0;
                                }
                            } else {
                                if (-up_move_y > fl.getHeight() / 4) {
                                    //说明移动到左上
                                    temp = channelNumbers[2];
                                    channelNumbers[2] = channelNumbers[0];
                                    channelNumbers[0] = temp;
                                    temp = 0;
                                }
                            }
                            break;
                        case 3:
                            if (-up_move_x > fl.getWidth() / 4) {
                                if (-up_move_y > fl.getHeight() / 4) {
                                    //说明移动到左上
                                    temp = channelNumbers[3];
                                    channelNumbers[3] = channelNumbers[0];
                                    channelNumbers[0] = temp;
                                    temp = 0;
                                } else {
                                    //说明移动到左下
                                    temp = channelNumbers[3];
                                    channelNumbers[3] = channelNumbers[2];
                                    channelNumbers[2] = temp;
                                    temp = 0;
                                }
                            } else {
                                if (-up_move_y > fl.getHeight() / 4) {
                                    //说明移动到右上
                                    temp = channelNumbers[3];
                                    channelNumbers[3] = channelNumbers[1];
                                    channelNumbers[1] = temp;
                                    temp = 0;
                                }
                            }
                            break;
                    }
                    setChannelName(channelNumbers);
                    //通知服务器更改通道号
                    NetController.displayMore(channelNumbers[0], channelNumbers[1], channelNumbers[2], channelNumbers[3]);

                }
                //抬起后位置归位  只是把文本换了 然后通知更改频道
                llChannel1.layout(0, 0, fl.getWidth() / 2 - 2, fl.getHeight() / 2 - 1);
                llChannel2.layout(fl.getWidth() / 2 + 2, 0, fl.getWidth(), fl.getHeight() / 2 - 1);
                llChannel3.layout(0, fl.getHeight() / 2 + 1, fl.getWidth() / 2 - 2, fl.getHeight());
                llChannel4.layout(fl.getWidth() / 2 + 2, fl.getHeight() / 2 + 1, fl.getWidth(), fl.getHeight());
                break;
        }
        return true;
    }

    @Override
    public void onDragView(int from, int to) {
//        for (int i = 0; i < mData.size(); i++) {
//            channelNumbers[i] = mData.get(i);
//        }
        int temp = channelNumbers[from];
        channelNumbers[from] = channelNumbers[to];
        channelNumbers[to] = temp;
        temp = 0;

        setChannelName(channelNumbers);
        for (int i = 0; i < channelNumbers.length; i++) {
            Log.e("adapter",i+"==="+Constant.channelNames[channelNumbers[i]]);
//            channelNumbers[i] = mData.get(i);
        }
        NetController.displayMore(channelNumbers[0], channelNumbers[1], channelNumbers[2], channelNumbers[3]);
    }

    private int clickChannel;

    @Override
    public void onClickItem(ArrayList<Integer> mData) {
//        for (int i = 0; i < mData.size(); i++) {
//            channelNumbers[i] = mData.get(i);
//        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.e("SettingFragment", "onDestroy");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.e("SettingFragment", "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LogUtil.e("SettingFragment", "onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);

    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.e("SettingFragment", "onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.e("SettingFragment", "onStop");
    }
}
